package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.InspectionProcess;


import com.zhentao.pojo.PlateRecord;

import com.zhentao.service.impl.InspectionProcessServiceImpl;
import com.zhentao.service.impl.PlateRecordServiceImpl;
import com.zhentao.service.impl.ResourceRecordServiceImpl;
import com.zhentao.service.impl.UserServiceImpl;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/CarInspection")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class CarInspectionController {
    //车牌记录
    @Autowired
    private PlateRecordServiceImpl plateRecordService;
    //用户
    @Autowired
    private UserServiceImpl userService;
    //检测流程
    @Autowired
    private InspectionProcessServiceImpl processService;
    //资源记录
    @Autowired
    private ResourceRecordServiceImpl resourceRecordService;
    @GetMapping("/Inspection")
    public Result Inspection(@RequestParam(value = "userId", defaultValue = "1") Integer userId) {
        try {
            QueryWrapper<PlateRecord> plateRecordQueryWrapper = new QueryWrapper<>();
            plateRecordQueryWrapper.eq("user_id", userId);
            List<PlateRecord> list = plateRecordService.list(plateRecordQueryWrapper);
            List<InspectionProcess> list1 = processService.list();
            for (PlateRecord plateRecord : list){
                for (InspectionProcess inspectionProcess : list1){
                    if (plateRecord.getId().equals(inspectionProcess.getPlateId())){
                        plateRecord.setInspectionProcess(list1);
                    }
                }
            }
            return Result.OK(list);
        } catch (Exception e) {
            System.err.println("查询检车记录失败: " + e.getMessage());
            e.printStackTrace();
            return Result.ERROR("查询检车记录失败: " + e.getMessage());
        }
    }

    //查看检车详情
    @GetMapping("/InspectionById")
    public Result InspectionById(@RequestParam Integer id) {
        InspectionProcess byId = processService.getById(id);
        List<PlateRecord> list = plateRecordService.list();
        for (PlateRecord plateRecord : list){
            if (plateRecord.getId().equals(byId.getPlateId())){
                byId.setPalteNumber(plateRecord.getPalteNumber());
            }
        }
        return Result.OK(byId);
    }
}
